package product

import (
	"context"
	"gitee.com/zyw0605688_admin/gozero/product_service/model"

	"gitee.com/zyw0605688_admin/gozero/product_service/internal/svc"
	"gitee.com/zyw0605688_admin/gozero/product_service/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductCreateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewProductCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductCreateLogic {
	return &ProductCreateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ProductCreateLogic) ProductCreate(req *types.ProductCreateRequest) (resp *types.ProductCreateResponse, err error) {
	item := model.Product{
		ProductName:   req.ProductName,
		ProductPrice:  req.ProductPrice,
		ProductDesc:   req.ProductDesc,
		ProductTypeId: req.ProductTypeId,
	}
	err = l.svcCtx.DB.Save(&item).Error
	if err != nil {
		return nil, err
	}
	productTypeName := ""
	err = l.svcCtx.DB.Raw("select product_type_name from product_type where id = ?", item.ProductTypeId).Scan(&productTypeName).Error
	if err != nil {
		return nil, err
	}
	resp = &types.ProductCreateResponse{
		Id:              item.Id,
		ProductName:     item.ProductName,
		ProductPrice:    item.ProductPrice,
		ProductDesc:     item.ProductDesc,
		ProductTypeId:   item.ProductTypeId,
		ProductTypeName: productTypeName,
		CreatedAt:       item.CreatedAt,
	}
	return
}
